
package mil.cdc.windows;

import java.awt.Color;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import mil.cdc.listeners.CustomMouseListener;
import mil.cdc.utils.CDCConstants;

import org.dyno.visual.swing.layouts.Bilateral;
import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
import org.dyno.visual.swing.layouts.Trailing;

//VS4E -- DO NOT REMOVE THIS LINE!
public class ErrorWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private JTextArea errorField;
	private JScrollPane errorPane;
	private JButton closeBtn;
	private boolean noError;

	public ErrorWindow() {
		initComponents();
	}

	private void initComponents() {
		setTitle("Error");
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/mil/cdc/data/ico/af.gif")));
		setResizable(false);
		setLayout(new GroupLayout());
		add(getJButton0(), new Constraints(new Bilateral(160, 159, 81), new Trailing(12, 33, 171)));
		add(getErrorPane(), new Constraints(new Leading(12, 385, 10, 10), new Leading(14, 145, 10, 10)));
		setSize(413, 243);
	}

	private JButton getJButton0() {
		if (closeBtn == null) {
			closeBtn = new JButton();
			closeBtn.setText("Close");
			closeBtn.addMouseListener(new CustomMouseListener(this, CDCConstants.CLOSE));
			closeBtn.setToolTipText("Close the currentWindow");
		}
		return closeBtn;
	}

	private JScrollPane getErrorPane() {
		if (errorPane == null) {
			errorPane = new JScrollPane();
			errorPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			errorPane.setEnabled(false);
			errorPane.setViewportView(getJTextArea0());
		}
		return errorPane;
	}

	private JTextArea getJTextArea0() {
		if (errorField == null) {
			errorField = new JTextArea();
			errorField.setEditable(false);
			errorField.setLineWrap(true);
			errorField.setWrapStyleWord(true);
			errorField.setEnabled(false);
			errorField.setDisabledTextColor(Color.black);
		}
		return errorField;
	}

	/**
	 * Displays an error message to the user.
	 * @param message Error message to display.
	 */
	public void displayErrorMessage(String message) {
		displayErrorMessage(message, "Error");
	}
	
	/**
	 * Displays an error message to the user with a custom 
	 * title.
	 * @param message Message to display. 
	 * @param title Title to display.
	 */
	public void displayErrorMessage(String message, String title) {
		displayErrorMessage(message, title, 
				this.getBounds().height, this.getBounds().width);
	}
	
	/**
	 * Displays an error message to the user with a custom title and 
	 * height and width
	 * @param message message to display	
	 * @param title title of the window
	 * @param height height of the window
	 * @param width width of the window
	 */
	public void displayErrorMessage(String message, String title, int height, int width) {
		this.setVisible(true);
		this.setTitle(title);
		this.setBounds(this.getBounds().x, this.getBounds().y, width, height);
		errorField.setText(message);
		noError = false;
	}
	
	/**
	 * Check to see if there is no error to display
	 * @return noError
	 */
	public boolean hasNoError() {
		return noError;
	}
	
	/**
	 * Sets the no error flag.
	 * @param noError True if there are no errors; false otherwise
	 */
	public void setNoError(boolean noError) {
		this.noError = noError;
	}
	
	public void reset() {
		this.setVisible(false);
		this.setTitle("Error");
		errorField.setText("");
		noError = true;
	}
}
